home *** CD-ROM | disk | FTP | other *** search
- Path: news.umbc.edu!not-for-mail
- From: schlein@umbc.edu (Jonas J. Schlein)
- Newsgroups: comp.lang.c
- Subject: Re: A weird thing about printf()
- Date: 17 Apr 1996 11:49:23 -0400
- Organization: University of Maryland Baltimore County
- Message-ID: <4l33u3$6vt@umbc9.umbc.edu>
- References: <4kflr2$5if@dewey.csun.edu>
- NNTP-Posting-Host: umbc9.umbc.edu
- NNTP-Posting-User: schlein
-
- |> The program :
- |>
- |> #include <stdio.h>
- |>
- |> int answer;
- |> main()
- |> {
- |> answer=2+2;
- |> printf("The answer is %d\n");
-
- You never tell printf() the variable you want to print! Change this line to:
-
- printf("The answer is %d\n", answer);
-
- |> return 0;
- |> }
- |>
- |> In printf(),I lost "answer" in the end,but it works,and give me the
- |> result 0.I wonder how the compiler handle this condition.Also,is this
- |> allow by C?
-
- Undefined behavior means anything can happen. Including even getting lucky
- and having it print what you expected.
-
- |> (by this, I mean is this a leagal usage in language itself,
- |> or just a mistake cause by the compiler?)
-
- It's certainly not the way printf() was intended to work. If you give it a
- %d it expects an int further along in the argument list.
- --
- "If it wasn't for C, we would be using BASI, PASAL, and OBOL."
-
- Jonas J. Schlein (schlein@gl.umbc.edu)
-